<template>
	<el-container>
		<el-aside width="240px">
			<el-container>
				<el-main class="nopadding">
					<blCategoryTree model="PageDesign" title="页面分类" ref="group" @node-click="groupClick" />
				</el-main>
			</el-container>
		</el-aside>
		<el-container>
			<el-header>
				<div class="left-panel">
					<div class="right-panel-search">
						<el-input v-model="search.keyword" placeholder="页面名称" clearable></el-input>
						<el-button type="primary" icon="el-icon-search" @click="upsearch">查询</el-button>
					</div>
				</div>
				<div class="right-panel">
					<div class="right-panel-search">

					</div>
				</div>
			</el-header>
			<div v-if="false">
				<el-card shadow="never">
					<sc-select-filter :data="state" :selected-values="selectedValues" :label-width="80"
						@on-change="stateChange"></sc-select-filter>
				</el-card>
			</div>
			<el-main class="nopadding">
				<taTableCard class="card-nopadding" hideAdd rowHeight="auto" :cardSize="['auto']" ref="table"
					:apiObj="$API.system.pageDesign.list" row-key="id">
					<template #default="{item}">
						<div
							style="display: flex;flex-direction: column; justify-content: space-between; position: relative;">
							<template v-if="item.page_preview">
								<div>
									<img style="max-width: 190px; max-height: 350px; object-fit: contain;"
										:src="item.page_preview+'?x-oss-process=image/resize,w_190&t='"
										:alt="item.title" />
								</div>
							</template>
							<el-button type="primary">
								{{item.page_name}}
							</el-button>
							<el-dropdown class="media-dropdown" trigger="click">
								<el-button icon="ArrowDown" @click.stop="" />
								<template #dropdown>
									<el-dropdown-menu>
										<el-dropdown-item>
											<el-button size="small" type="success" icon="Link"
												@click="handleCopy(item.page_preview)">
												链接
											</el-button>
										</el-dropdown-item>
										<el-dropdown-item>
											<el-button size="small" type="primary" icon="Edit" @click="page_edit(item)">
												装修
											</el-button>
										</el-dropdown-item>
									</el-dropdown-menu>
								</template>
							</el-dropdown>


						</div>
					</template>
				</taTableCard>

			</el-main>
		</el-container>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>

</template>

<script>
	import saveDialog from './save'
	import previewDialog from './save'

	import scSelectFilter from '@/components/scSelectFilter'

	export default {
		name: 'page.design',
		components: {
			saveDialog,
			scSelectFilter
		},
		data() {
			return {
				state: [{
					title: "素材类型",
					key: "type",
					options: [{
							label: "全部",
							value: null
						},
						{
							label: "图片",
							value: "image",
							icon: "el-icon-picture-filled"
						},
						{
							label: "视频",
							value: "video",
							icon: "el-icon-video-camera-filled"
						},
						{
							label: "文档",
							value: "document",
							icon: "el-icon-document"
						}
					]
				}],
				selectedValues: {
					state: [""],
					type: [""]
				},
				dialog: {
					save: false
				},
				selection: [],
				search: {
					keyword: null,
					dateRange: null
				}
			}
		},
		methods: {
			async handleCopy(text) {
				try {
					await navigator.clipboard.writeText(text);
					this.$message.success("复制成功！");
				} catch (err) {
					this.$message.error("复制失败，请手动复制");
				}
			},
			stateChange(e) {
				var params = {
					state: e
				}
				this.$refs.table.upData(params)
			},
			//添加
			add() {
				this.dialog.save = true;
				this.$nextTick(() => {
					const checkedNode = this.$refs.group.getCurrentNode();
					this.$refs.saveDialog.open().setData({
						category_id: checkedNode.id || null
					});
				})
			},
			table_preview(row) {
				this.dialog.preview = true
				this.$nextTick(() => {
					this.$refs.previewDialog.open('preview').setData(row)
				})
			},
			//编辑
			table_edit(row) {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},
			//查看
			table_show(row) {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('show').setData(row)
				})
			},
			page_edit(row) {
				this.$router.push('/pageDesign/edit?id=' + row.id)
			},
			//删除
			async table_del(row, index) {
				var reqData = {
					id: row.id
				}
				var res = await this.$API.system.goods.remove.post(reqData);
				if (res.code == 0) {
					this.$refs.table.tableData.splice(index, 1);
					this.$message.success(res.msg)
				} else {
					this.$alert(res.message, "提示", {
						type: 'error'
					})
				}
			},
			//批量删除
			async batch_del() {
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning'
				}).then(() => {
					const loading = this.$loading();
					this.selection.forEach(item => {
						this.$refs.table.tableData.forEach((itemI, indexI) => {
							if (item.id === itemI.id) {
								this.$refs.table.tableData.splice(indexI, 1)
							}
						})
					})
					loading.close();
					this.$message.success("操作成功")
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection) {
				this.selection = selection;
			},
			//树点击事件
			groupClick(data) {
				var params = {
					category_id: data.id
				}
				this.$refs.table.upData(params)
			},
			//搜索
			upsearch() {
				this.$refs.table.upData(this.search)
			},
			//本地更新数据
			handleSuccess(data, mode) {
				if (mode == 'add') {
					this.$refs.table.refresh()
				} else if (mode == 'edit') {
					this.$refs.table.refresh()
				}
			}
		}
	}
</script>

<style scoped>
	.media-dropdown {
		position: absolute;
		right: 0px;
		bottom: 0px;
		display: none;
	}

	:deep(.el-card__body:hover) .media-dropdown {
		display: block;
	}

	.card-nopadding :deep(.el-card__body) {
		padding: 0 !important;
	}
</style>
